package com.shop.cloud.JPush;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;


public class MsgPushUtils {
	
	/**
	 * @param 极光推送
	 * @param gmc
	 * @param text 推送内容
	 * @return PushResult
	 * @throws APIRequestException 
	 * @throws APIConnectionException 
	 */
	public static PushResult addJPush(String text) throws APIConnectionException, APIRequestException{
		
        String masterSecret = "09e1854b0f507eb4fa3860dd";
		
		String appKey = "74d8fc8ca2e594bd6d11b2da";
		
		int maxRetryTimes = 3;
		
		JPushClient jpushClient = new JPushClient(masterSecret, appKey, maxRetryTimes);
		
		PushPayload payload = MsgPushUtils.buildPushObject_all_all_alert(text);
		
		PushResult result = jpushClient.sendPush(payload);
		
		return result;
		
	}
	
	/**
	 * @param 极光推送
	 * @param gmc
	 * @param text 推送内容
	 * @return PushResult
	 * @throws APIRequestException 
	 * @throws APIConnectionException 
	 */
	public static PushResult addJPush(String text, String alias) throws APIConnectionException, APIRequestException{
		
        String masterSecret = "09e1854b0f507eb4fa3860dd";
		
		String appKey = "74d8fc8ca2e594bd6d11b2da";
		
		int maxRetryTimes = 3;
		
		JPushClient jpushClient = new JPushClient(masterSecret, appKey, maxRetryTimes);
		
		PushPayload payload = MsgPushUtils.buildPushObject_all_alias_alert(text, alias);
		
		PushResult result = jpushClient.sendPush(payload);
		
		return result;
		
	}
	
	/**
	* 快捷地构建推送对象：所有平台，所有设备，内容为 ALERT 的通知
	*/
	public static PushPayload buildPushObject_all_all_alert(String test) {
	return PushPayload.alertAll(test);
	}
	
	/**
	*构建推送对象：所有平台，推送目标是别名为 "alias1"，通知内容为 ALERT。 
	*/
	public static PushPayload buildPushObject_all_alias_alert() {
	return PushPayload.newBuilder()
	.setPlatform(Platform.all())
	.setAudience(Audience.alias("alias1"))
	.setNotification(Notification.alert(""))
	.build();
	}
	
	/**
	*构建推送对象：所有平台，推送目标是别名为 "alias1"，通知内容为 ALERT。 
	*/
	public static PushPayload buildPushObject_all_alias_alert(String text, String alias) {
	return PushPayload.newBuilder()
	.setPlatform(Platform.all())
	.setAudience(Audience.alias(alias))
	.setNotification(Notification.alert(text))
	.build();
	}
	
	/**
	* 构建推送对象：平台是 iOS，推送目标是 "tag1", "tag_all" 的交集
	* 推送内容同时包括通知与消息 - 通知信息是 ALERT，角标数字为 5，通知声音为 "happy"，并且附加字段 from = "JPush"；
	* 消息内容是 MSG_CONTENT。通知是 APNs 推送通道的
	* 消息是 JPush 应用内消息通道的。APNs 的推送环境是“生产”（如果不显式设置的话，Library 会默认指定为开发）
	*/
	public static PushPayload buildPushObject_android_tag_alertWithTitle() {
	return PushPayload.newBuilder()
	.setPlatform(Platform.android())
	.setAudience(Audience.tag("tag1"))
	.setNotification(Notification.android("ALERT", "TITLE", null))
	.build();
	} 
	
	
	/**
	* 构建推送对象：平台是 Andorid 与 iOS，推送目标是 （"tag1" 与 "tag2" 的并集）且（"alias1" 与 "alias2" 的并集）
	* 推送内容是 - 内容为 MSG_CONTENT 的消息，并且附加字段 from = JPush。
	*/
	public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
	return PushPayload.newBuilder()
	.setPlatform(Platform.android_ios())
	.setAudience(Audience.newBuilder()
	.addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
	.addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
	.build())
	.setMessage(Message.newBuilder()
	.setMsgContent("MSG_CONTENT")
	.addExtra("from", "JPush")
	.build())
	.build();
	}

}
